﻿using Nini.Config;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace UltimateMp3TaggerUI
{
    public partial class FormSettings : Form
    {
        public FormSettings()
        {
            InitializeComponent();

            LoadSettings();
        }

        private void LoadSettings()
        {
            IConfigSource configSource = new IniConfigSource("umtagger.ini");

            IConfig configSection = configSource.Configs["Proxy"];

            string enabled = configSection.Get("enable");

            bool isProxyEnabled = false;

            Boolean.TryParse(enabled, out isProxyEnabled);

            this.checkBoxProxy.Checked = isProxyEnabled;

            string address = configSection.Get("address");
            string port = configSection.Get("port", "8080");
            string domain = configSection.Get("domain");
            string username = configSection.Get("user");
            string password = configSection.Get("password");

            this.textBoxHost.Text = address;
            this.textBoxPort.Text = port;
            this.textBoxDomain.Text = domain;
            this.textBoxUser.Text = username;
            this.textBoxPassword.Text = password;

        }

        

        private void buttonUpdates_Click(object sender, EventArgs e)
        {
            WebProxy proxy = null;

            bool isProxyEnabled = this.checkBoxProxy.Checked;

            if (isProxyEnabled)
            {
                int port = 8080;

                string address = Uri.IsWellFormedUriString(textBoxHost.Text, UriKind.RelativeOrAbsolute) ? textBoxHost.Text : Dns.GetHostName();

                Int32.TryParse(textBoxPort.Text, out port);

                proxy = new WebProxy(address, port);

                proxy.Credentials = new NetworkCredential(
                    this.textBoxUser.Text,
                    this.textBoxPassword.Text,
                    this.textBoxDomain.Text);

            }

            UMTUtility.ShowUpdateSoftwareMessageAsync(true, proxy);

        }



        private void buttonSave_Click(object sender, EventArgs e)
        {
            IConfigSource configSource = new IniConfigSource("umtagger.ini");

            IConfig configSection = configSource.Configs["Proxy"];

            configSection.Set("enable", this.checkBoxProxy.Checked);
            configSection.Set("address", this.textBoxHost.Text);
            configSection.Set("port", this.textBoxPort.Text);
            configSection.Set("domain", this.textBoxDomain.Text);
            configSection.Set("user", this.textBoxUser.Text);
            configSection.Set("password", this.textBoxPassword.Text);

            configSource.Save();

            this.Close();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            //this.Close();
        }
    }
}
